package com.icss.trans.accept.httppoll;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

/**
 * 
 * @ClassName: AcceptParam
 * @Description:
 * @author ICSS
 * @date 2015-3-25 下午3:14:06
 * 
 */
public class AcceptParam {

    /**
     * <pre>
     * 请求获取文件列表时：
     * 1、dataTransType（数据类型ID），必选；
     * 2、appCode（应用类型ID），必选；
     * 3、key 身份识别码，用于权限校验；
     * 
     * 请求获取数据文件时：
     * 1、dataTransType（数据类型ID），必选；
     * 1、  appCode（应用类型ID），必选；
     * 2、  key 身份识别码，用于权限校验；
     * 3、  fileName（数据文件名称）。
     * </pre>
     */
    public static final String DATA_FILE_LIST = "list";

    public static final String DATA_FILE_INFO = "data";

    private String dataTransType;

    private String appCode;

    private String key;

    private String fileName;

    /**
     * @Description: 
     * @param builder
     */
    private AcceptParam(Builder builder) {
        this.dataTransType = builder.dataTransType;
        this.appCode = builder.appCode;
        this.key = builder.key;
        this.fileName = builder.fileName;
    }

    /**
     * @Description:
     */
    public AcceptParam() {
    }

    public static class Builder {

        private String dataTransType;

        private String appCode;

        private String key;

        private String fileName;

        public Builder dataTransType(String dataTransType) {
            this.dataTransType = dataTransType;
            return this;
        }

        public Builder appCode(String appCode) {
            this.appCode = appCode;
            return this;
        }

        public Builder key(String key) {
            this.key = key;
            return this;
        }

        public Builder fileName(String fileName) {
            this.fileName = fileName;
            return this;
        }

        /**
         * 
         * @Description: buildAcceptParam参数对象
         * @return
         */
        public AcceptParam build() {
            if (StringUtils.isEmpty(dataTransType)) {
                throw new IllegalArgumentException("param[dataTransType] is null");
            }
            if (StringUtils.isEmpty(appCode)) {
                throw new IllegalArgumentException("param[appCode] is null");
            }
            if (StringUtils.isEmpty(key)) {
                throw new IllegalArgumentException("param[key] is null");
            }
            return new AcceptParam(this);
        }
    }

    /**
     * 
     * @Description: 构建键值对表单对象
     * @return
     * @throws
     */
    public List<NameValuePair> buildNameValuePair() {
        List<NameValuePair> formParams = new ArrayList<NameValuePair>();
        addBasicNameValuePair(formParams, "dataTransType", this.getDataTransType());
        addBasicNameValuePair(formParams, "appCode", this.getAppCode());
        addBasicNameValuePair(formParams, "key", this.getKey());
        addBasicNameValuePair(formParams, "fileName", this.getFileName());
        return formParams;
    }

    private void addBasicNameValuePair(List<NameValuePair> contextParams, String param, String value) {
        if (StringUtils.isNotEmpty(value)) {
            contextParams.add(new BasicNameValuePair(param, value));
        }
    }

    public String getDataTransType() {
        return dataTransType;
    }

    public void setDataTransType(String dataTransType) {
        this.dataTransType = dataTransType;
    }

    public String getAppCode() {
        return appCode;
    }

    public void setAppCode(String appCode) {
        this.appCode = appCode;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }

}
